package cn.edu.tju.elm.service;

import cn.edu.tju.elm.model.DeliveryAddress;
import cn.edu.tju.elm.repository.AddressRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * ClassName: AddressService
 * Package: cn.edu.tju.elm.service
 * Description:
 *
 * @Author Niu Wenhang
 * @Create 2025/9/17 20:25
 * @Version 1.0
 */
@Service
@Transactional
public class AddressService {
    private final AddressRepository addressRepository;

    public AddressService(AddressRepository addressRepository) {
        this.addressRepository = addressRepository;
    }

    public DeliveryAddress addAddress(DeliveryAddress deliveryAddress) {
        return addressRepository.save(deliveryAddress);
    }

    public List<DeliveryAddress> getAddressesByUserId(Long userId) {
        return addressRepository.findByCustomerIdAndDeletedFalse(userId);
    }

    public Optional<DeliveryAddress> getAddressById(Long id) {
        return addressRepository.findById(id);
    }

    public DeliveryAddress updateAddress(DeliveryAddress deliveryAddress) {
        return addressRepository.save(deliveryAddress);
    }

    public void deleteAddress(Long id) {
        addressRepository.deleteById(id);
    }

    public List<DeliveryAddress> getAddressesByContentContaining(String content, Long userId) {
        return addressRepository.findByAddressContainingAndCustomerIdAndDeletedFalse(content, userId);
    }

    public List<DeliveryAddress> getAddressesByContactNameContaining(String contactName, Long userId) {
        return addressRepository.findByContactNameContainingAndCustomerIdAndDeletedFalse(contactName, userId);
    }

    public List<DeliveryAddress> getAddressesByPhone(String phone, Long userId) {
        return addressRepository.findByContactTelAndCustomerIdAndDeletedFalse(phone, userId);
    }
}
